<?php require_once('Connections/CrossFit.php'); ?>
<?php
//initialize the session
if (!isset($_SESSION)) {
  session_start();
}

// ** Logout the current user. **
$logoutAction = $_SERVER['PHP_SELF']."?doLogout=true";
if ((isset($_SERVER['QUERY_STRING'])) && ($_SERVER['QUERY_STRING'] != "")){
  $logoutAction .="&". htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_GET['doLogout'])) &&($_GET['doLogout']=="true")){
  //to fully log out a visitor we need to clear the session varialbles
  $_SESSION['MM_Username'] = NULL;
  $_SESSION['MM_UserGroup'] = NULL;
  $_SESSION['PrevUrl'] = NULL;
  unset($_SESSION['MM_Username']);
  unset($_SESSION['MM_UserGroup']);
  unset($_SESSION['PrevUrl']);
	
  $logoutGoTo = "index.php";
  if ($logoutGoTo) {
    header("Location: $logoutGoTo");
    exit;
  }
}
?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
$checkboxMember = $_POST['membernames'];
$checkboxMember2 = $_POST['membernames2'];

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
	for ($i=0; $i<sizeof($checkboxMember); $i++){
  $insertSQL = sprintf("INSERT INTO Attendance (`Date`,`Class`, Class_Time, Member_Name,Not_Listed_Name) VALUES (%s, %s, %s, '".$checkboxMember[$i]."', %s)",
                       GetSQLValueString($_POST['datepicker'], "date"),
                       GetSQLValueString($_POST['classtyperadio'], "text"),
                       GetSQLValueString($_POST['classtimesradio'], "text"),
                       GetSQLValueString($_POST['namenotlisted'], "text"));

  mysql_select_db($database_CrossFit, $CrossFit);
  $Result1 = mysql_query($insertSQL, $CrossFit) or die(mysql_error());
	}
	for ($i=0; $i<sizeof($checkboxMember2); $i++){
  $insertSQL = sprintf("INSERT INTO Attendance (`Date`,`Class`, Class_Time, Member_Name,Not_Listed_Name) VALUES (%s, %s, %s, '".$checkboxMember2[$i]."', %s)",
                       GetSQLValueString($_POST['datepicker'], "date"),
                       GetSQLValueString($_POST['classtyperadio'], "text"),
                       GetSQLValueString($_POST['classtimesradio'], "text"),
                       GetSQLValueString($_POST['namenotlisted'], "text"));

  mysql_select_db($database_CrossFit, $CrossFit);
  $Result1 = mysql_query($insertSQL, $CrossFit) or die(mysql_error());
	}
}



	


mysql_select_db($database_CrossFit, $CrossFit);
$query_MemberList = "SELECT Name FROM Members ORDER BY Name ASC";
$MemberList = mysql_query($query_MemberList, $CrossFit) or die(mysql_error());
$row_MemberList = mysql_fetch_assoc($MemberList);
$totalRows_MemberList = mysql_num_rows($MemberList);
$query_MemberList = "SELECT Name FROM Members WHERE ID <= 25 ORDER BY Name ASC";
$MemberList = mysql_query($query_MemberList, $CrossFit) or die(mysql_error());
$row_MemberList = mysql_fetch_assoc($MemberList);
$totalRows_MemberList = mysql_num_rows($MemberList);

mysql_select_db($database_CrossFit, $CrossFit);
$query_MemberListGreaterThan25 = "SELECT Name FROM Members WHERE ID > 25 ORDER BY Name ASC";
$MemberListGreaterThan25 = mysql_query($query_MemberListGreaterThan25, $CrossFit) or die(mysql_error());
$row_MemberListGreaterThan25 = mysql_fetch_assoc($MemberListGreaterThan25);
$totalRows_MemberListGreaterThan25 = mysql_num_rows($MemberListGreaterThan25);

mysql_select_db($database_CrossFit, $CrossFit);
$query_ClassTypes = "SELECT ClassType FROM ClassType ORDER BY ID ASC";
$ClassTypes = mysql_query($query_ClassTypes, $CrossFit) or die(mysql_error());
$row_ClassTypes = mysql_fetch_assoc($ClassTypes);
$totalRows_ClassTypes = mysql_num_rows($ClassTypes);

mysql_select_db($database_CrossFit, $CrossFit);
$query_ClassTimes = "SELECT Class_Times FROM classtimes ORDER BY ID ASC";
$ClassTimes = mysql_query($query_ClassTimes, $CrossFit) or die(mysql_error());
$row_ClassTimes = mysql_fetch_assoc($ClassTimes);
$totalRows_ClassTimes = mysql_num_rows($ClassTimes);

mysql_select_db($database_CrossFit, $CrossFit);
$query_MembersListView = "SELECT Name FROM member_list WHERE `Name_Filter` = 1 ORDER BY Name ASC";
$MembersListView = mysql_query($query_MembersListView, $CrossFit) or die(mysql_error());
$row_MembersListView = mysql_fetch_assoc($MembersListView);
$totalRows_MembersListView = mysql_num_rows($MembersListView);

mysql_select_db($database_CrossFit, $CrossFit);
$query_MembersListViewM = "SELECT Name FROM member_list WHERE `Name_Filter` = 0 ORDER BY Name ASC";
$MembersListViewM = mysql_query($query_MembersListViewM, $CrossFit) or die(mysql_error());
$row_MembersListViewM = mysql_fetch_assoc($MembersListViewM);
$totalRows_MembersListViewM = mysql_num_rows($MembersListViewM);

mysql_select_db($database_CrossFit, $CrossFit);
$query_CUR_DATE = "SELECT CURDATE() FROM classtimes";
$CUR_DATE = mysql_query($query_CUR_DATE, $CrossFit) or die(mysql_error());
$row_CUR_DATE = mysql_fetch_assoc($CUR_DATE);
$totalRows_CUR_DATE = mysql_num_rows($CUR_DATE);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<link rel="stylesheet" type="text/css" href="26Attendance.css" />

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Attendance - CrossFit 26</title>
<link type="text/css" href="jqueryUI/css/cupertino/jquery-ui-1.8.20.custom.css" rel="stylesheet" />

<link rel="stylesheet" type="text/css" href="button.css">
		<script type="text/javascript" src="jqueryUI/js/jquery-1.7.2.min.js"></script>
		<script type="text/javascript" src="jqueryUI/js/jquery-ui-1.8.20.custom.min.js"></script>
		<script>
	$(function() {
		
		$( "#datepicker" ).datepicker({ minDate: -30, maxDate: +0, dateFormat: "yy-mm-dd" });
		
		
	});
	
	
	
	
	
	
	</script>
    
    
    
    <style>
	#feedback { font-size: 1.4em; }
	#selectable .ui-selecting { background: #FECA40; }
	#selectable .ui-selected { background: #F39814; color: white; }
	#selectable { list-style-type: none; margin: 0; padding: 0; width: 60%; }
	#selectable li { margin: 3px; padding: 0.4em; font-size: 1.4em; height: 18px; }
	</style>
	<script>
	$(function() {
		$( "#selectable" ).selectable({
			stop: function() {
				var result = $( "#select-result" ).empty();
				$( ".ui-selected", this ).each(function() {
					var index = $( "#<?php echo $row_ClassTimes['Class_Times']; ?>" ).val(this );
					result.append( ( "<?php echo $row_ClassTimes['Class_Times']; ?> " ) );
					
				});
			}
		});
	});
	</script>

</head>

<body>
<p><img src="CrossFit26_26Fearless Dark.png" width="176" height="120" />
<h3>Attendance Entry</h3>
<p><a href="attendancelist.php">go to Historical Attendance List</a>  </p>
<p><div id="datepicker2"></div><!-- End demo --></p>

<!--<p><a href="addwodscore.php">Add WOD Scores to Today's Attendance </a>(BETA, not working yet)</p>-->


<?php 
$checkboxMember3 = $_POST['membernames'];
$checkboxMember4 = $_POST['membernames2'];
 if (count($_POST)>0) echo "<h3>Recently entered names:</h3> <br>"; 
for ($i=0; $i<sizeof($checkboxMember3); $i++){
print_r($checkboxMember3[$i].'<br>');
}
for ($i=0; $i<sizeof($checkboxMember4); $i++){
print_r($checkboxMember4[$i].'<br>');
}


?>
<form action="<?php echo $editFormAction; ?>" method="POST" name="form1" id="form1">
  <table width="600" border="0">
    <tr>
      <td>
      <p><label for="datepicker">Select Class Date: </label><br /><input name="datepicker" type="text" id="datepicker" value="<?php echo $row_CUR_DATE['CURDATE()']; ?>" readonly="readonly">
      
      
      
      
      
      </p>
      <p><strong>Select the class type: 
        </strong><br />
        <?php do { ?>
          <input name="classtyperadio" type="radio" id="<?php echo $row_ClassTypes['ClassType']; ?>" value="<?php echo $row_ClassTypes['ClassType']; ?>" />
          <label for="<?php echo $row_ClassTypes['ClassType']; ?>"><?php echo $row_ClassTypes['ClassType']; ?></label>
           &nbsp;&nbsp;&nbsp;
          <?php } while ($row_ClassTypes = mysql_fetch_assoc($ClassTypes)); ?>
      </p>
        
        <p>
          <strong>Select class time:</strong><br />
          
          <?php do { ?>
          
            <input type="radio" name="classtimesradio" id="<?php echo $row_ClassTimes['Class_Times']; ?>" value="<?php echo $row_ClassTimes['Class_Times']; ?>" />
            <label for="<?php echo $row_ClassTimes['Class_Times']; ?>"><?php echo $row_ClassTimes['Class_Times']; ?></label>
            <br />
            
            <?php } while ($row_ClassTimes = mysql_fetch_assoc($ClassTimes)); ?>
      
      </p></td>
    </tr>
  </table>
  
  <p><strong>Athlete Names</strong></p>
  <p>If you don't see a name in the list, click <a href="addathlete.php">Add Athlete</a>.</p>
  <table width="500" border="0">
    <tr>
      <td valign="top" ><?php do { ?>
          <table  border="0"   cellpadding="6" cellspacing="3">
            <tr>
              <td valign="top" height="25" width="250">
                <input type="checkbox" name="membernames[]"  value="<?php echo $row_MembersListView['Name']; ?>" id="<?php echo $row_MembersListView['Name']; ?>" />
               <label for="<?php echo $row_MembersListView['Name']; ?>"> <?php echo $row_MembersListView['Name']; ?></label>
                <br /></td>
            </tr>
          </table>
      <?php } while ($row_MembersListView = mysql_fetch_assoc($MembersListView)); ?></td>
      <td valign="top"><?php do { ?>
          <table  border="0"   cellpadding="6" cellspacing="3">
            <tr>
              <td valign="top" height="25" width="250"><input type="checkbox" name="membernames2[]"  value="<?php echo $row_MembersListViewM['Name']; ?>" id="<?php echo $row_MembersListViewM['Name']; ?>"/>
               <label for="<?php echo $row_MembersListViewM['Name']; ?>"> <?php echo $row_MembersListViewM['Name']; ?></label><br /></td>
            </tr>
          </table>
      <?php } while ($row_MembersListViewM = mysql_fetch_assoc($MembersListViewM)); ?></td>
    </tr>
  </table>    
<br />
    
    <input class="button" type="submit" name="Submit" id="Submit" value="Submit" />&nbsp;&nbsp;&nbsp;
    <input class="button" type="reset" name="reset" id="reset" value="Reset" />
    <br />
  </p>
<p>&nbsp;</p>
  <input type="hidden" name="MM_insert" value="form1" />
</form>
<p><a href="<?php echo $logoutAction ?>">Log Out</a></p>
</body>
</html>
<?php
mysql_free_result($MemberList);

mysql_free_result($MemberListGreaterThan25);

mysql_free_result($ClassTypes);

mysql_free_result($ClassTimes);

mysql_free_result($MembersListView);

mysql_free_result($MembersListViewM);

mysql_free_result($CUR_DATE);
?>